<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/img/heima.png" alt="">
        <h1>电商后台管理系统</h1>
      </div>
      <div>
        <span class="activeUser">当前用户：{{username}}</span>
        <el-button type="info" plain @click="logout">退出</el-button>
      </div>

    </el-header>
    <el-container>
      <!-- 左侧列表项 -->
      <el-aside :width="isCollapse ? '64px' :'200px' ">
        <div class="toggle-collapse" @click="toggleCollapse">|||</div>
        <el-menu :default-active="activePath" router :collapse-transition="false" unique-opened :collapse="isCollapse" background-color="#333744" text-color="#fff" active-text-color="#409eff">
          <!-- 使用双重for循环渲染菜单列表 -->
          <el-submenu :index="item.id+'' " :key="item.id" v-for="item in menuList">
            <!-- 一级模板 -->
            <template slot="title"><i :class="icons[item.id]"></i><span>{{item.authName}}</span></template>
            <!-- 二级模板 -->
            <el-menu-item @click="saveActivePath('/'+subItem.path)" :index="'/'+subItem.path" :key="subItem.id" v-for="subItem in item.children">
              <template slot="title"><i class="el-icon-menu"></i><span>{{subItem.authName}}</span></template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧面板 -->
      <el-main>
        <!-- home主页路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 用户名
      username: '',
      menuList: [],
      // icon图标
      // icons: [
      //   'iconfont icon-users',
      //   'iconfont icon-tijikongjian',
      //   'iconfont icon-shangpin',
      //   'iconfont icon-danju',
      //   'iconfont icon-baobiao'
      // ],
      icons: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 判定菜单栏是否折叠
      isCollapse: false,
      // 活动状态下的菜单项路由链接
      activePath: ''
    };
  },
  methods: {
    // 退出登录
    async logout() {
      // 弹出信息提示框
      const confirmResult = await this.$confirm('您确认退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err);
      // console.log(confirmResult);
      if (confirmResult !== 'confirm') return;
      // // 清除token
      // window.sessionStorage.removeItem('token');
      // // 清除activePath
      // window.sessionStorage.removeItem('activePath');
      // // 清除username
      // window.sessionStorage.removeItem('username');
      window.sessionStorage.clear();
      // 跳到登录路由
      this.$router.push('/login');
    },

    // 获取左侧菜单列表数据
    async getMenuList() {
      const res = await this.$axios.get('/menus');
      if (res.meta.status !== 200 && res.meta.msg !== '无效token') { return this.$message.error('获取菜单列表失败！'); }
      this.menuList = res.data;
      // this.menuList.forEach((item, index) => (item.icon = this.icons[index]));
      // console.log(this.menuList);
    },

    // 控制菜单栏展开还是折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },

    // 保存活动状态路由路径
    saveActivePath(activePath) {
      // 把活动状态的路由路径 保存在本地
      window.sessionStorage.setItem('activePath', activePath);
      // 把活动状态的路由路径赋值给数据
      this.activePath = activePath;
    }
  },
  created() {
    // 在vue实例初始化完成后获取左侧菜单列表数据
    this.getMenuList();
    // 浏览器前进后退按钮 设置 活动路径为当前路由页面的路径
    window.addEventListener('popstate', () => {
      // console.log(this.$route.path);
      this.activePath = this.$route.path;
      window.sessionStorage.setItem('activePath', this.$route.path);
    });
    // 激活链接路径(为了刷新界面的时候，取出，然后再次高亮)
    this.activePath = window.sessionStorage.getItem('activePath');
    // 获取登录用户名
    this.username = window.sessionStorage.getItem('username');
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    h1 {
      margin-left: 15px;
      font-weight: 400;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    background-color: #333744;
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-collapse {
  cursor: pointer;
  user-select: none;
  height: 24px;
  line-height: 24px;
  text-align: center;
  color: #fff;
  font-size: 10px;
  letter-spacing: 0.2em;
  background-color: #4a5064;
}
.activeUser {
  margin-right: 10px;
}
</style>
